capture log close
clear all
set more off

*	************************************************************************
* 	File-Name: 	replication_EP.do
*	Date:  		06/07/2017
*	Author: 	Semee Yoon
*	Data Used:  Sample_svy_vill.dta / public_complete.dta / withsales.dta
*	Output		None
*	Purpose:   	.do file to replicate the findings in 
* 				"Can Product Demonstrations Create Markets for Sustainable Energy Technology? A Randomized Controlled Trial in Rural India"
*	************************************************************************

*	************************************************************************
* 	0. Setting up the data
*	************************************************************************

* Change this link to your working directory where you want to work.
cd "/Users/serene/"
global inSHS="~/Dropbox/gift/solar/shs_j_s/DataCode_EXPERIMENTAL/"
global outSHS="~/Dropbox/gift/solar/shs_j_s/FINAL"

* Change this link to your working directory where the dataset is located.
use $inSHS/Sample_svy_vill.dta

*	************************************************************************
* 	1. Summary statistics
*	************************************************************************

*** Table 5 Covariate Balance

use $inSHS/Sample_svy_vill.dta

label var gender_vill "Female Household Heads (\%)"
label var hhsize_vill "Household size"
label var electricity_vill "Has electricity (\%)"
label var lightingperday_vill "Lighting hours per day"
label var fuelcost_kero_vill "Monthly kerosene cost (INR)"
label var rupeesspent "Monthly household expenditure (1,000 INR)"

eststo clear
estpost summ gender_vill hhsize_vill electricity_vill lightingperday_vill fuelcost_kero_vill rupeesspent if Treatment==0 
eststo A
estpost summ gender_vill hhsize_vill electricity_vill lightingperday_vill fuelcost_kero_vill rupeesspent if Treatment==1 
eststo B
estpost ttest gender_vill hhsize_vill electricity_vill lightingperday_vill fuelcost_kero_vill rupeesspent, by(Treatment)
esttab C
esttab A B C using $outSHS/Tables/cov_bal1.tex, label booktabs cells(mean(fmt(a2)) sd(par fmt(a2))) mtitle("Control(1)" "Treatment (2)" "Difference (3)") nonum replace 


*** Table A1 
use $inSHS/public_complete, clear

label var sales "Total number of SHS sales"
replace sales =0 if wave==0

ttest gender, by(Treatment)
ttest hhsize, by(Treatment)
ttest electricity, by(Treatment)
ttest lightingper, by(Treatment)
ttest fuelcost_kero_vill, by(Treatment)
ttest rupeesspent, by(Treatment)

eststo clear
local vars1 gender_vill hhsize_vill electricity_vill lightingperday_vill fuelcost_kero_vill rupeesspentpermonth_vill sales indiaandsolar_vill gridandsubsidy_vill govtorpriv_vill interestedinowning_vill shsbuyestimate_vill privgoodforhome_vill privgoodforbiz_vill knowshs_vill sawshs_vill
estpost summ `vars1' if  wave==0
eststo A
estpost summ `vars1' if Treatment==0 & wave==1
eststo B
estpost summ `vars1' if Treatment==1 & wave==1
eststo C
esttab A B C using $outSHS/Tables/su_hhchar_wave.tex, label booktabs cells(mean(fmt(a2)) sd(par fmt(a2))) mtitle("Baseline" "Endline-control" "Endline-treatment") nonum replace 

*	************************************************************************
* 	2. Main analysis
*	************************************************************************


use $inSHS/Sample_svy_vill.dta

*** Table 6
* OLS
reg sales Treatment
est tab, p(%12.10g) 
eststo S1
reg sales Treatment gender hhsize electricity lighting
est tab, p(%12.10g)
eststo S2
reg sales Treatment gender hhsize electricity lighting fuelcost_kero_vill rupeesspent
est tab, p(%12.10g) 
eststo S3
esttab S1 S2 S3 using $outSHS/Tables/main_reg.tex, replace label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) ar2(%9.3f) nogaps nomtitle

* Negative binomial
nbreg sales Treatment
est tab, p(%12.10g)
eststo N1
nbreg sales Treatment gender hhsize electricity lighting
est tab, p(%12.10g)
eststo N2
nbreg sales Treatment gender hhsize electricity lighting fuelcost_kero_vill rupeesspent
est tab, p(%12.10g)
eststo N3
esttab N1 N2 N3 using $outSHS/Tables/nbreg.tex, replace label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) nogaps nomtitle

*** Table 7
use $inSHS/public_complete, clear

label var sales "Total number of SHS sales"
replace sales =0 if wave==0

xtset villagecode wave

eststo clear
foreach var of varlist indiaandsolar_vill gridandsubsidy_vill govtorpriv_vill interestedinowning_vill shsbuyestimate_vill privgoodforhome_vill privgoodforbiz_vill {
	xtreg `var' Treatment i.wave, fe i(villagecode)
	est tab, p(%12.10g)
	eststo `var'1, title(`var')
	}
esttab indiaandsolar_vill1 gridandsubsidy_vill1 govtorpriv_vill1 interestedinowning_vill1 shsbuyestimate_vill1 privgoodforhome_vill1 privgoodforbiz_vill1 using $outSHS/Tables/reg.tex, replace keep(Treatment) indicate("Wave FE = 1.wave")  label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) nogaps  nomtitle

* use p value returned from each regression = est tab, p(%12.10g) 
clear
set obs 1
generate pvalue =  .1464262924 in 1
set obs 2
replace pvalue = .844008569  in 2
set obs 3
replace pvalue = .2308288433 in 3
set obs 4
replace pvalue = .4916593822 in 4
set obs 5
replace pvalue = .1800587016 in 5
set obs 6
replace pvalue = .0552486268 in 6
set obs 7
replace pvalue = .097248777 in 7

sort pvalue
gen k=(_N+1)-_n
generate sidak=1-(1-pvalue)^k
replace  sidak=sidak[_n-1] if sidak[_n-1]>sidak in 2/L

use $inSHS/public_complete, clear

label var sales "Total number of SHS sales"
replace sales =0 if wave==0

xtset villagecode wave

eststo clear
local controls gender_vill hhsize_vill electricity_vill lightingperday_vill fuelcost_kero_vill rupeesspentpermonth_vill
foreach var of varlist indiaandsolar_vill gridandsubsidy_vill govtorpriv_vill interestedinowning_vill shsbuyestimate_vill privgoodforhome_vill privgoodforbiz_vill {
	xtreg `var' Treatment `controls' i.wave, fe i(villagecode)
		est tab, p(%12.10g)
	eststo `var'2, title(`var')
	}
esttab indiaandsolar_vill2 gridandsubsidy_vill2 govtorpriv_vill2 interestedinowning_vill2 shsbuyestimate_vill2 privgoodforhome_vill2 privgoodforbiz_vill2 using $outSHS/Tables/reg_controls.tex, replace keep(Treatment) indicate("Wave FE = 1.wave" "Controls=`controls'")  label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) nogaps  nomtitle

clear
set obs 1
generate pvalue =   .0372293464 in 1
set obs 2
replace pvalue =  .9715021484  in 2
set obs 3
replace pvalue =  .1353833326  in 3
set obs 4
replace pvalue = .3577970032  in 4
set obs 5
replace pvalue = .2556603897 in 5
set obs 6
replace pvalue =  .0055513316  in 6
set obs 7
replace pvalue =  .013115752   in 7

sort pvalue
gen k=(_N+1)-_n
generate sidak=1-(1-pvalue)^k
replace  sidak=sidak[_n-1] if sidak[_n-1]>sidak in 2/L


*	************************************************************************
* 	4. Additional analysis
*	************************************************************************

*** Table A2
use $inSHS/public_complete, clear
xtreg knowshs_vill Treatment i.wave, fe
eststo know
xtreg sawshs_vill Treatment i.wave, fe
eststo saw
esttab know saw using $outSHS/Tables/reg_mechanism.tex, replace keep(Treatment) indicate("Wave FE = 1.wave") label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) nogaps mtitle
 
*** Table A3
use $inSHS/Sample_svy_vill_weighted, clear
gen sales_weighted= sales/no_hh
reg sales_weighted Treatment gender hhsize electricity lighting fuelcost_kero_vill rupeesspent
est tab, p(%12.10g)
eststo S4
esttab S4 using $outSHS/Tables/weighted_reg.tex, replace label booktabs star(* 0.10 ** 0.05 *** 0.01) se b(%9.3f) ar2(%9.3f) nogaps nomtitle

*	************************************************************************
* 	5. Graphs
*	************************************************************************

*** Figure 1: Scatter plots of the percentage of attendees (upper) and those reporting willingness to buy SHS (lower) relative to the total village sales
use $inSHS/withsales.dta, clear
label var attendees "Village-level number of attendees"
label var sales "Village-level total SHS sales"
twoway scatter sales attendees, scheme(s1color)
graph export $outSHS/Figures/sales_scatter.eps, replace

label var pplwhoshowedwillignesstobuyshs "Village-level number of attendees willing to purchase SHS"
label var sales "Village-level total SHS sales"
twoway scatter sales pplwhoshowedwillignesstobuyshs, scheme(s1color)
graph export $outSHS/Figures/sales_scatter21.eps, replace
